//
//  HMCircleAnimator.m
//  转场动画测试
//
//  Created by 宇宙执政 on 16/9/27.
//  Copyright © 2016年 yujianbinMAC. All rights reserved.
//

#import "HMCircleAnimator.h"

@interface HMCircleAnimator()<UIViewControllerAnimatedTransitioning,CAAnimationDelegate>{
    
    BOOL _isPresent;
    
    __weak id <UIViewControllerContextTransitioning> _transitionContext;
}

@end


@implementation HMCircleAnimator


#pragma mark - UIViewControllerTransitioningDelegate
// 提供展现转场动画的对象  为当前类self - 所以需要实现UIViewControllerAnimatedTransitioning代理
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    _isPresent = YES;
    
    return self;
}


// 提供解除转场动画的对象  为当前类self  - 所以需要实现UIViewControllerAnimatedTransitioning代理
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    _isPresent = NO;
    
    return self;
}


#pragma mark - UIViewControllerAnimatedTransitioning
//转场动画时长
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    
    return  0.25;
}

//转场动画的核心类，由程序员实现，用来实现转场动画
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    //0，记录当前转场上下文
    _transitionContext = transitionContext;
    
    //1,得到转场动画的容器视图，通过这个容器，才能完成两个view之间的动画
    UIView *containerView = [transitionContext containerView];
    
    //2,如果是展现的话，fromView是nil,toView有值
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    NSLog(@"----fromView = %@",[fromView class]);
    
    //3,如果是结束展现的话，toView是nil,fromView有值
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    NSLog(@"----toView = %@",[toView class]);
    
    UIView *view = _isPresent ? toView : fromView;

    [containerView addSubview:view];
    
    [self animator:view];
    
//    //完成转场 --很重要
//    [transitionContext completeTransition:YES];
    
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    //完成转场 --很重要
    [_transitionContext completeTransition:YES];
    
}

-(void)animator:(UIView *)view{
    
    //1,创建CAShapeLayer对象
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    
    CGFloat radius = 30;
    CGFloat rightMargin = 15;
    CGFloat topMargin = 28;
    CGFloat viewWidth = view.bounds.size.width;
    CGFloat viewHeight = view.bounds.size.height;
    
    //2，创建开始的bezierpath路径
    CGRect beginRect = CGRectMake(viewWidth-radius-rightMargin, topMargin, radius, radius);
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:beginRect];
    
    //3,设置layer的路径，设置view的遮罩
    layer.path = beginPath.CGPath;
    view.layer.mask = layer;
    
    //4,计算结束路径
    CGFloat maxRadius = sqrt(viewWidth*viewWidth + viewHeight*viewHeight);
    //中心点不变的情况下，放大矩形，
    CGRect endRect = CGRectInset(beginRect, -maxRadius, -maxRadius);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    //5,核心动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
    
    anim.duration = [self transitionDuration:_transitionContext];
    
    //判断是否是展现
    if (_isPresent) {
        anim.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
        anim.toValue = (__bridge id _Nullable)(endPath.CGPath);
    }else{
        anim.fromValue = (__bridge id _Nullable)(endPath.CGPath);
        anim.toValue = (__bridge id _Nullable)(beginPath.CGPath);
    }

    //保持转场
    //设置填充模式
    anim.fillMode = kCAFillModeForwards;
    //完成之后不删除
    anim.removedOnCompletion = NO;
    
    anim.delegate = self;
    
    [layer addAnimation:anim forKey:nil];
    
}


@end
